package com.hzhc.controller.baseinfo;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.ProductName;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.ProductNameService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.ProductNameVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 铁路品名表 前端控制器
 * </p>
 *
 * @author shizj
 * @since 2024-08-30
 */
@RestController
@RequestMapping("/productName")
@RequiredArgsConstructor
public class ProductNameController extends BaseController {
    private final ProductNameService productNameService;

    @RequestMapping("/getList")
    @ResponseBody
    public ResultUtil<List<ProductNameVo>> getList(@RequestBody ProductNameVo vo) {
        Page<ProductNameVo> page = new Page<>(vo.getPageIndex(),vo.getPageSize());
        List<ProductNameVo> list = productNameService.getList(page, vo);
        return ResultUtil.build(list,page.getTotal(),page.getPages(), ResultUtilCodeEnum.SUCCESS);
    }

    @RequestMapping("/addRet")
    @SysTemLog(operModul = "基础资料-品名维护和限制添加",operType = "添加",operDesc = "品名添加功能",modular="PC")
    public ResultUtil<T> addRet(HttpServletRequest request, @RequestBody ProductName productName) {
        UserDto user= TokenUtil.getUser(request);
        productName.setUserId(user.getId());
        return productNameService.addRet(productName);
    }

    @RequestMapping("/updateRet")
    @SysTemLog(operModul = "基础资料-品名维护和限制编辑",operType = "编辑",operDesc = "品名编辑功能",modular="PC")
    public ResultUtil<T> updateRet(HttpServletRequest request,@RequestBody ProductName productName) {
        UserDto user= TokenUtil.getUser(request);
        productName.setUpdateUserId(user.getId());
        return productNameService.updateRet(productName);
    }

    @RequestMapping("/delete")
    @SysTemLog(operModul = "基础资料-品名维护和限制删除",operType = "删除",operDesc = "品名删除功能",modular="PC")
    public ResultUtil<T> deleteRet(@RequestBody ProductName productName) {
        return productNameService.deleteRet(productName);
    }

    /**
     * 校验设备名称
     */
    @RequestMapping("/checkTradeName")
    public ResultUtil<T> checkTradeName(@RequestBody ProductName productName){
        String field = "trade_name";
        ProductName m =productNameService.checkTradeName(field, productName.getTradeName(), productName.getId());
        if (m!=null){
            return ResultUtil.build(ResultUtilCodeEnum.TRADE_NAME_EXIST);
        }else{
            return ResultUtil.ok();
        }
    }
}
